package org.six.background.service.impl;

import com.alibaba.druid.sql.visitor.functions.Substring;
import org.six.background.mapper.InshopMapper;
import org.six.background.mapper.ShopMapper;
import org.six.background.mapper.ShoptypeMapper;
import org.six.background.mapper.UserMapper;
import org.six.background.pojo.*;
import org.six.background.service.InshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class InshopServiceImpl implements InshopService {

    @Autowired(required = false)
    private InshopMapper inshopMapper;
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private ShopMapper shopMapper;

    @Autowired(required = false)
    private ShoptypeMapper shoptypeMapper;
    @Override
    public List<Inshop> inshopshow() {
        System.out.println("值");
        System.out.println(inshopMapper.inshopshow());

        return inshopMapper.inshopshow();
    }

    @Override
    @Transactional(timeout = 1000)
    public String inshopAdd(String usname,int inidcard,int usphone,String shopname,int innum) {
        System.out.println("outttt");
        System.out.println(usname);
        System.out.println(inidcard);
        System.out.println(usphone);
        System.out.println(shopname);
        System.out.println(innum);

        UserExample userExample=new UserExample();
        UserExample.Criteria uc=userExample.createCriteria();
        uc.andUsnameEqualTo(usname);
        List<User> users=userMapper.selectByExample(userExample);
        //
        System.out.println("users"+users);
        //
        int usid=users.get(0).getUsid();
        System.out.println("usid="+usid);
        ShopExample shopExample=new ShopExample();
        ShopExample.Criteria sc=shopExample.createCriteria();
        sc.andShopnameEqualTo(shopname);
        List<Shop> shops=shopMapper.selectByExample(shopExample);
        int shopid=shops.get(0).getShopid();
        int shopnum=shops.get(0).getShopnum()+innum;
        Shop shop= shopMapper.selectByPrimaryKey(shopid);
        shop.setShopnum(shopnum);
        shopMapper.updateByPrimaryKeySelective(shop);
        System.out.println("更改的数量："+shop.getShopnum());

        Inshop inshop=new Inshop();
        Date date=new Date();
        inshop.setInaddtime(date);
        inshop.setInnum(innum);
        inshop.setInidcard(inidcard);
        inshop.setUsid(usid);
        inshop.setInusname(usname);
        inshop.setShopid(shopid);
        //
        System.out.println(inshop);

        int i=inshopMapper.insertSelective(inshop);

        if (i==1){
            return "ok";
        }
        return "no";
    }

    @Override
    public Map inchart() {
        List<String> date=new ArrayList<String>();
        List<String> year=new ArrayList<String>();
        List<String> month=new ArrayList<String>();

        String datee;
        String yearr;
        String monthh;
        List<Inshop> inshops = inshopMapper.selectByExample(null);
        for (int i = 0; i < inshops.size(); i++) {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-dd/MM");
            datee=simpleDateFormat.format(inshops.get(i).getInaddtime());
            date.add(datee);
            yearr=datee.substring(0, datee.indexOf("-"));
            monthh=datee.substring(8);
            System.out.println("年份："+yearr);
            System.out.println("月份："+monthh);
            year.add(yearr);
            month.add(monthh);
        }
            for(int i  =  0 ; i < year.size()  -   1 ; i ++ )  {
                for  ( int  j  =  year.size()  -   1 ; j  >  i; j -- )  {
                    if  (year.get(j).equals(year.get(i)))  {
                        year.remove(j);
                    }
                }
            }
        for(int i  =  0 ; i < month.size()  -   1 ; i ++ )  {
            for  ( int  j  =  month.size()  -   1 ; j  >  i; j -- )  {
                if  (month.get(j).equals(month.get(i)))  {
                    month.remove(j);
                }
            }
        }



        List<Shop> shops = shopMapper.selectByExample(null);
        List<Shoptype> shoptypes = shoptypeMapper.selectByExample(null);
        Map map=new HashMap();
        map.put("year",year);
        map.put("month",month);
        map.put("shops",shops);
        map.put("shoptypes",shoptypes);
        System.out.println(map.toString());
        return map;
    }

    @Override
    public Map inshopAll(int page, int pagesize) {
        int count = inshopMapper.countByExample(null);
        double pageMax=Math.ceil(count/pagesize)+1;
        Map pages=new HashMap();
        pages.put("page",page);
        pages.put("pagesize",pagesize);
        pages.put("count",count);
        pages.put("pagemax",pageMax);
        page=page>(int)(pageMax)?(int)(pageMax):page;

        Map map1=new HashMap();
        map1.put("index",(page-1)*pagesize);
        map1.put("pagesize",pagesize);

        List<Inshop> inshops = inshopMapper.inshopAll(map1);

        Map map=new HashMap();
        map.put("inshops",inshops);
        map.put("pages",pages);
        System.out.println(map.toString());
        return map;
    }

}
